package com.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * a线程打印1到10
 * b线程打印11到20
 * ...
 */
public class ThreadTest01 {
    private volatile static int i = 1;

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ThreadTest01.method();
                }
            }).start();
        }
    }

    public static void method() {
        boolean toUnlock = false;
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
            while (!toUnlock) {
                System.out.println(Thread.currentThread().getName() + "_" + i);
                if (i % 10 == 0) {
                    toUnlock = true;
                }
                i++;
            }
        } finally {
            lock.unlock();
        }
    }

}
